import sys

import requests
from loguru import logger

from const import ENV_DIR
from lib.parser import read_files


class Base(object):
    def mer_send_request(self, api_name, data: dict):
        if self._MER_HOST != '':
            data['url'] = self._MER_HOST + data['url']
        logger.info(f'接口名称:{api_name} | 请求方式:{data}')
        res = requests.request(**data)
        logger.info(f'接口名称:{api_name} | 响应方式:{res.text}')
        return res.json()

    def for_send_request(self, api_name, data: dict):
        if self._FOR_HOST != '':
            data['url'] = self._FOR_HOST + data['url']
        logger.info(f'接口名称:{api_name} | 请求方式:{data}')
        res = requests.request(**data)
        logger.info(f'接口名称:{api_name} | 响应方式:{res.text}')
        return res.json()

    def plat_send_request(self, api_name, data: dict):
        if self._PLAT_HOST != '':
            data['url'] = self._PLAT_HOST + data['url']
        logger.info(f'接口名称:{api_name} | 请求方式:{data}')
        res = requests.request(**data)
        logger.info(f'接口名称:{api_name} | 响应方式:{res.text}')
        return res.json()


class Login(Base):

    def __init__(self):
        configuration_file = str(ENV_DIR / "test.yaml")
        self.res = read_files(configuration_file)
        path = r'D:\dev\python\python\ProductApiTest\env\test.yaml'
        self.res = read_files(path)
        # host
        self._MER_HOST = self.res['merchant']['HOST']
        self._FOR_HOST = self.res['user']['HOST']
        self._PLAT_HOST = self.res['platform']['HOST']
        print(self._PLAT_HOST)
        # token
        self._mer_token = self.__mer_login()['data']['token']
        self._for_token = self.__for_login()['data']['token']
        self._plat_token = self.__plat_login()['data']['token']

    # 登录 商家端
    def __mer_login(self):
        data = {
            "url": '/api/admin/merchant/login',
            "method": "post",
            "headers": {'Content-Type': 'application/json'},
            'json': {
                'account': self.res['merchant']['USERNAME'],
                'pwd': self.res['merchant']['PASSWORD'],
                'captchaVO': {},
                'captcha': {
                    'captchaVerification': '',
                    'secretKey': '',
                    'token': '',
                }
            }}
        return self.mer_send_request(sys._getframe().f_code.co_name, data)

    # 用户端
    def __for_login(self):
        data = {
            "url": '/api/front/login/mobile/password',
            "method": "post",
            "headers": {'Content-Type': 'application/json'},
            'json': {
                'phone': self.res['user']['USERNAME'],
                'password': self.res['user']['PASSWORD'],
                'spreadPid': 0,
            }}
        return self.for_send_request(sys._getframe().f_code.co_name, data)

    # 平台端
    def __plat_login(self):
        data = {
            "url": '/api/admin/platform/login',
            "method": "post",
            "headers": {'Content-Type': 'application/json'},
            'json': {
                'account': self.res['platform']['USERNAME'],
                'pwd': self.res['platform']['PASSWORD'],
                'captchaVO': {},
            }}
        return self.plat_send_request(sys._getframe().f_code.co_name, data)
